package com.mccken.demo.io.netty.codec;

import com.mccken.demo.io.netty.support.BaseRpcBean;
import com.mccken.demo.io.netty.util.HessianSerializer;
import com.mccken.demo.io.netty.util.MySerializer;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * 编码器
 * 
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-04-30 17:42
 **/
@Slf4j
public class MyEncoder extends MessageToByteEncoder<BaseRpcBean> {
	
	private static MySerializer hessianSerializer = new HessianSerializer();

	@Override
	protected void encode(ChannelHandlerContext ctx, BaseRpcBean baseRpcBean, ByteBuf out) throws Exception {
		log.info("encode baseRpcBean:{}", baseRpcBean);
		byte[] bytes = hessianSerializer.serialize(baseRpcBean);
		out.writeInt(bytes.length);
		out.writeBytes(bytes);
	}
}

